package com.yq.ylmall.acl.controller;

import com.yq.ylmall.acl.service.MenuService;
import com.yq.ylmall.acl.service.UserService;
import com.yq.ylmall.acl.service.ValidateCodeService;
import com.yq.ylmall.common.utils.AuthContextUtil;
import com.yq.ylmall.dto.acl.LoginDto;
import com.yq.ylmall.model.acl.User;
import com.yq.ylmall.result.Result;
import com.yq.ylmall.vo.acl.LoginVo;
import com.yq.ylmall.vo.acl.ValidateCodeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.yq.ylmall.result.ResultCode;
import com.yq.ylmall.vo.acl.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "登录管理接口")
@RestController
@RequestMapping("/admin/system/index")
public class IndexController {

    @Autowired
    private UserService userService ;

    @Autowired
    private MenuService menuService;
    @Autowired
    private ValidateCodeService validateCodeService;
    @ApiOperation(value = "获取动态菜单")
    @GetMapping("/menus")
    public Result menus() {
        List<MenuVo> sysMenuVoList =  menuService.findUserMenuList() ;
        return Result.build(sysMenuVoList , ResultCode.SUCCESS) ;
    }
    /**
     * 1、请求登陆的login
     */
    @ApiOperation(value = "登录接口")
    @PostMapping(value = "/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = userService.login(loginDto) ;
        return Result.build(loginVo , ResultCode.SUCCESS) ;
    }


    /**
     * 获取验证码
     * @return
     */
    @GetMapping(value = "/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo , ResultCode.SUCCESS) ;
    }

    /**
     * 2 获取用户信息
     */
    @GetMapping(value = "/getUserInfo")
    public Result<User> getUserInfo() {
        return Result.build(AuthContextUtil.get(),ResultCode.SUCCESS);
    }

    /**
     * 3 退出
     */
    @GetMapping(value = "/logout")
    public Result logout(@RequestHeader(value = "token") String token) {
        userService.logout(token) ;
        return Result.build(null , ResultCode.SUCCESS) ;
    }
}
